﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.ComponentModel;
using System.Windows.Forms;

namespace AMS.UI.Menu
{
    /// <summary>
    /// 窗体显示的通知对象
    /// </summary>
    public abstract class FormShowAction : IMenuClickAction
    {
        /// <summary>
        /// 处理点击事件之前的函数，事件可以被拦截
        /// </summary>
        /// <param name="menuItem"></param>
        /// <param name="handled"></param>
        public abstract void BeforeProcessMenuClick(object menuItem, ref bool handled);

        /// <summary>
        /// 处理点击事件
        /// </summary>
        /// <param name="menuItem"></param>
        public abstract void OnMenuClick(object menuItem);

        /// <summary>
        /// 处理点击事件之后的函数
        /// </summary>
        /// <param name="menuItem"></param>
        public virtual void AfterProcessMenuClick(object menuItem)
        {
        }

        /// <summary>
        /// 点击事件之前的的主处理函数
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="handled"></param>
        protected void BeforeClick(string tag, ref bool handled)
        {
            if (string.IsNullOrEmpty(tag))
            {
                handled = true;
                return;
            }

            // 通知主窗体
            IFormShowNotifier notifier = FrmMainModel.Instance.MainForm as IFormShowNotifier;
            if (notifier != null)
            {
                MenuClickEventArgs args = new MenuClickEventArgs();
                notifier.OnMenuItemClicked(tag, args);
                handled = args.Handled;
            }
        }

        /// <summary>
        /// 点击事件的主处理函数
        /// </summary>
        /// <param name="tag"></param>
        protected void DoMenuClick(string tag)
        {
            Type t = ObjectUtility.GetType(tag);
            if (t != null)
            {
                Form workForm = FrmMainModel.Instance.CheckAndShowForm(t, tag);

                // 通知主窗体
                IFormShowNotifier notifier = FrmMainModel.Instance.MainForm as IFormShowNotifier;
                if (notifier != null)
                {
                    notifier.OnShowNotify(workForm, null);
                }
            }
            else
            {
                // TODO: if debug version throw exceptions
            }
        }
    }
}
